import {
    Body,
    Controller,
    Get,
    Param,
    Post,
    Query,
    UseGuards,
    UseInterceptors
} from "@nestjs/common"
import { AuthGuard } from "@nestjs/passport";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { existsSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from "fs";
import { AttendanceService } from "src/attendance/attendance.service";
import { DeptService } from "src/dept/dept.service";
import { DingtalkService } from "src/dingtalk/dingtalk.service";
import { UseLog } from "src/log/useLog.decorator";
import { NoticeService } from "src/notice/notice.service";
import { RoleEnum } from "src/role/role.enum";
import { RoleService } from "src/role/role.service";
import { SystemService } from "src/system/system.service";
import { CurrentUser } from "src/user/user.decorator";
import { UserService } from "src/user/user.service";
import { UserSys } from "src/user/userSys.entity";
import { IsNull } from "typeorm";
import { TestService } from "./test.service";
import * as moment from "moment";

@Controller("api/test")
@ApiTags("测试接口")
export class TestController {
    constructor(
        private readonly userService: UserService,
        private readonly deptService: DeptService,
        private readonly roleService: RoleService,
        private readonly systemService: SystemService,
        private readonly dingtalkService: DingtalkService,
        private readonly attendanceService: AttendanceService,
        private readonly noticeService: NoticeService,
        private readonly testService: TestService
    ) { }

    @Get("all")
    all() {
        return this.testService.criticize.find({
            order: {
                id: "desc",
            },
            relations: ["user"]
        })
    }

    @Get("basic")
    @UseGuards(AuthGuard("jwt"))
    @ApiOperation({ summary: "首页信息" })
    async basic(
        @CurrentUser() user: UserSys
    ) {
        const result = {} as any;
        const message = await this.noticeService.notice.find({
            where: {
                user: { id: user.id }
            },
            relations: ["target"],
            order: { id: "desc" }
        })
        result.message = message;
        return result;
    }

    @Post("criticize")
    @UseGuards(AuthGuard("jwt"))
    @ApiOperation({ summary: "意见反馈" })
    crticicize(
        @Body() { context },
        @CurrentUser() user: UserSys
    ) {
        const entity = this.testService.criticize.create({ user, context });
        return this.testService.criticize.save(entity)
    }


    async addSystem() {
        let system = await this.systemService.system.findOne({ where: { id: 1 } });
        if (!system) system = await this.systemService.create({
            name: "test",
            persons: [],
            dept: [],
            comment: "",
            id: 0,
            createAt: undefined,
            updateAt: undefined,
        })
        await this.userService.create({ phone: "15511110000", name: "测试", role: RoleEnum.director }, system);
    }
    async addUser(phone, name) {
        const user = await this.userService.create({
            phone,
            name,
        })
        user.role = RoleEnum.boos;
        this.userService.userSys.save(user);
        return user;
    }

    public test() {
        return "sbppk"
    }

}